import getData from '../getData/api.js'
import {Message} from "element-ui";

const state = {
    tableData: [],
    pageCount:1,
    roleList:[],
    agencyIdList:[],
    leadArr:[],
    classArr:[],
    leadName:[],
    roleArr:[],
}

const getters = {
    testGetter(state) {
        return state.tableData;
    }
}

const mutations = {
    testMutation(state, data) {
        function renderTime(date) {
            let dateee = new Date(date).toJSON();
            return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
        }
        state.pageCount = data.pages
        for(let i =0 ;i<data.records.length;i++) {
            let resData = {};
            resData.id = data.records[i].userId;//用户id
            resData.jobNumber = data.records[i].jobNumber;
            resData.wetchatId = data.records[i].wetchatId;
            resData.userName = data.records[i].userName;
            resData.phone = data.records[i].phone;
            resData.email = data.records[i].email;
            resData.sex = data.records[i].sex;
            resData.position = data.records[i].position;
            resData.leadJobNum = data.records[i].leadJobNum;
            resData.leadName = data.records[i].leadName;
            resData.authFlag = data.records[i].authFlag;
            resData.roleCode=data.records[i].roleCode;
            resData.createTime = renderTime(data.records[i].creationTime);
            resData.updateTime = renderTime(data.records[i].modifyTime);
            resData.deptName = data.records[i].deptName;
            if('1'==resData.sex){
                resData.sex="男";
            }
            if('0'==resData.sex){
                resData.sex="女";
            }
            state.tableData.push(resData);
        }
    },
    LEAD(state, data) {
        for(let i =0 ;i<data.length;i++) {
            let resData = {};
            resData.value = data[i].jobNumber;
            resData.label = data[i].userName+'('+data[i].jobNumber+")";
            resData.userName = data[i].userName;
            // resData.userName = data[i].userName;
            state.leadArr.push(resData);
        }
    },
    class(state, data) {
        for(let i =0 ;i<data.length;i++) {
            let resData = {};
            resData.value = data[i].deptCode;
            resData.label = data[i].deptName;
            resData.userId = data[i].deptLead;
            state.classArr.push(resData);
        }
    },
    role(state, data) {
        for(let i =0 ;i<data.length;i++) {
            let resData = {};
            resData.value = data[i].roleCode;
            resData.label = data[i].roleName;
            // resData.userId = data[i].deptLead;
            state.roleArr.push(resData);
        }
    },
    leadName(state, data) {
      state.leadName=data;
    },
};

const actions = {
    //查询调用的方法
    getDataAction({state, commit}, params) {
        return getData.systemUserData(params)
            .then((res) => {
                state.tableData=[]
                return commit('testMutation', res.data);
            })
            .catch((err)=>{
                // console.log(err)
            })
    },
    auditingAction({state, commit}, params) {
        return getData.auditing(params)
            .then((res) => {
                state.tableData=[]
                return commit('testMutation', res.data);
            })
            .catch((err)=>{
                // console.log(err)
            })
    },
    getLead({state, commit}, params) {
        return getData.getLeadData(params)
            .then((res) => {
                state.leadArr=[];
                return commit('LEAD', res.data);
            })
            .catch((err)=>{
                // console.log(err)
            })
    },
    getRole({state, commit}, params) {
        return getData.getRoleData(params)
            .then((res) => {
                state.roleArr=[];
                return commit('role', res.data);
            })
            .catch((err)=>{
                // console.log(err)
            })
    },
    delUserAction({state, commit}, params) {
        return getData.delData(params)
            .then((res) => {
                if (res.code == 200){
                    Message.success(res.message);
                }else if(res.code != 200){
                    Message.error(res.message);
                }
            })
            .catch((err)=>{
                // console.log(err)
            })
    },
    getLeadName({state, commit}, params) {
        return getData.getLeadNameData(params)
            .then((res) => {
                return commit('leadName', res.data);
            })
            .catch((err)=>{
                // console.log(err)
            })
    },
    getClass({state, commit}, params) {
        return getData.selectlist(params)
            .then((res) => {
                state.classArr=[];
                return commit('class', res.data);
            })
            .catch((err)=>{
                // console.log(err)
            })
    },
    changeAction({state, commit}, params) {
        return getData.changeUser(params)
            .then((res) => {
                if (res.code == 200){
                    Message.success(res.message);
                }else if(res.code != 200){
                    Message.error(res.message);
                }
                return commit('testMutation', res.data);
            })
            .catch((err)=>{
                // console.log(err)
            })
    },
}

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
}